package br.com.nooclix.ptt.ws.application;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import br.com.nooclix.ptt.core.application.TermApplication;
import br.com.nooclix.ptt.core.domain.product.curated.Term;
import br.com.nooclix.ptt.core.infrastructure.util.ApplicationContextRegisterCore;
import br.com.nooclix.ptt.ws.adapter.Adapter;
import br.com.nooclix.ptt.ws.adapter.impl.TermAdapter;

@Path("/term")
public class TermRestApplication {

	private TermApplication termApplication;
	private Adapter<Term> adapter;

	public TermRestApplication() {
		termApplication = ApplicationContextRegisterCore.getBean(
				"termApplication", TermApplication.class);
		adapter = new TermAdapter();
	}

	@POST
	@Path("/create")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	public Response persist(@FormParam("description") String description) {

		Response response = null;
		Term term = new Term();
		term.setDescription(description);
		try {
			termApplication.persist(term);
			response = Response.ok().build();
		} catch (Exception e) {
			response= Response.status(Status.INTERNAL_SERVER_ERROR).build();
		}		
		return response;
	}
	
	@GET
	@Path("/list/{offset}")
	public Response listAll(@PathParam("offset") int offset){
		List<Term> termsList = termApplication.listAll(offset);
		String json = adapter.convert(termsList);
		return Response.ok(json).build();
	}
	

}
